<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/3/5 0005
 * Time: 15:27
 */

namespace app\common\widget;


use app\common\library\CatchException;
use app\common\library\CommonFun;
use Exception;

/**
 * Class WidgetSearchForm
 * @package app\common\widget
 *
 * @method WidgetInput addTextInput($sort=0)    添加一个text表单
 * @method WidgetSelect addSelectInput($sort=0) 添加一个select表单
 * @method WidgetButton addButton($sort=0)      添加一个按钮
 * @method WidgetSearchForm setFormAction($action) 设置表单action地址
 * @method WidgetSearchForm setFormGetMethod() 设置表单提交方式为GET
 * @method WidgetSearchForm setFormPostMethod() 设置表单提交方式为POST
 *
 */
class WidgetSearchForm extends WidgetCommon
{
    /**
     * 表单元素列表
     * @var array
     */
    private $inputList = [];

    /**
     * 搜索表单按钮组
     * @var array
     */
    private $buttonList = [];

    /**
     * 构造方法, 渲染挂件
     * @return mixed
     * @throws \think\Exception
     * @throws Exception
     */
    function render()
    {
        $this->beforeRender();
        // 分配元素列表
        if ( CommonFun::array_is_assoc( $this->inputList ) === false ) ksort( $this->inputList );
        self::$view->assign('inputList', $this->inputList);
        // 分配按钮组
        if ( CommonFun::array_is_assoc( $this->buttonList ) === false ) ksort($this->buttonList);
        self::$view->assign('buttonList', $this->buttonList);

        // 渲染
        return $this->fetch();
    }

    /**
     * Before Render
     * @return $this
     * @throws Exception
     */
    private function beforeRender() {
        // 检测
        if ( !$this->inputList ) throw new Exception("搜索表单没有From元素", CatchException::WidgetErrorException);
        if ( !$this->buttonList ) throw new Exception("搜索表单没有按钮组", CatchException::WidgetErrorException);

        // Render InputList
        foreach ( $this->inputList as &$v ) {
            if ( $v instanceof WidgetInput) {
                $widthList = $v->getWidth();
                $widthList = implode(" ", $widthList);
                $inputHtml =  $v->render();
                $ele = "<div class=\"{$widthList} form-group\">{$inputHtml}</div>";
                $v = $ele;
            }
        }

        // Render Button
        foreach ( $this->buttonList as &$b ) {
            if ( $b instanceof WidgetButton) $b = $b->render();
        }

        return $this;
    }

    /**
     * 初始化数据
     * @throws \think\Exception
     */
    protected function initialize()
    {
        parent::initialize();
        // 设置ID
        $this->setId('search_form');
        // 设置form属性
        $this->setDomAttr('method', 'GET');
        $this->setDomAttr('action', '');
        $this->setDomAttr('role', 'form');
        // 设置默认按钮
        $this->addButton()->setId('resetButton')->setButtonTypeReset()->setButtonStyleDanger()->setButtonText("重置");
        $this->addButton()->setId('refreshButton')->setButtonTypeDefault()->setButtonStyleInfo()->setButtonText("刷新");
        $this->addButton()->setId('submitButton')->setButtonTypeSubmit()->setButtonStyleSuccess()->setButtonText("提交");
    }

    /**
     * Call Undefine Method
     * @param $name
     * @param $arguments
     * @return mixed
     * @throws \think\Exception
     */
    public function __call($name, $arguments)
    {
        // 匹配添加Input
        if ( $res = preg_match("/^add([A-Za-z]*)Input/",$name,$match) ) {
            $type = $match[1];
            if ( $type == "Text" ) {
                $obj = new WidgetInput();
            }else{
                $class_name = 'Widget'.$type;
                $class = __NAMESPACE__."\\{$class_name}";
                if ( !class_exists($class) ) $this->throwException( "搜索表单Input元素不存在" );
                $obj = new $class();
            }
            $sort = isset($arguments[0]) ? $arguments[0] : 0;
            // 设置搜索表单Input默认宽度
            $obj->setWidthColXs(2)->setWidthColSm(2)->setWidthColLg(2)->setWidthColMd(2);
            // Set Input
            $this->setInput( $obj, $sort );
            return $obj;
        }

        // add Button
        if ( $name == "addButton" ) {
            $buttonObj = new WidgetButton();
            $sort = isset($arguments[0]) ? $arguments[0] : 0;
            $this->setButton($buttonObj, $sort);
            return $buttonObj;
        }

        // set action
        if ( $name == "setFormAction" ) {
            $this->setDomAttr('action', $arguments[0]);
            return $this;
        }

        // set method get
        if ( $name == "setFormGetMethod" ) {
            $this->setDomAttr('method', 'GET');
            return $this;
        }

        // set method post
        if ( $name == "setFormPostMethod" ) {
            $this->setDomAttr('method', 'POST');
            return $this;
        }

        return $this;
    }

    /**
     * 设置一个input元素
     * @param WidgetInput $input
     * @param int $sort
     * @return $this
     */
    private function setInput( WidgetInput &$input, $sort=0) {
        if ( $sort ) {
            $this->inputList[(int)$sort] = $input;
        }else{
            $this->inputList[] = $input;
        }
        return $this;
    }

    /**
     * 设置一个按钮
     * @param WidgetButton $button
     * @param int $sort
     * @return $this
     */
    private function setButton( WidgetButton &$button, $sort=0 ) {
        if ( $sort ) {
            $this->buttonList[(int)$sort] = $button;
        }else{
            $this->buttonList[] = $button;
        }
        return $this;
    }



}